from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.viewlets.common import ViewletBase

from zope.component import getMultiAdapter
from Acquisition import aq_inner, aq_parent
from Products.CMFPlone import utils

class GlobalSectionsViewlet(ViewletBase):
    render = ViewPageTemplateFile('sections.pt')

    def update(self):
        portal_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_portal_state')
        context_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_context_state')
        actions = context_state.actions()
        portal_tabs_view = getMultiAdapter((self.context, self.request),
                                           name='portal_tabs_view')
        self.portal_tabs = portal_tabs_view.topLevelTabs(actions=actions)

        selectedTabs = self.context.restrictedTraverse('selectedTabs')
        self.selected_tabs = selectedTabs('index_html',
                                          self.context,
                                          self.portal_tabs)
        self.selected_portal_tab = self.selected_tabs['portal']
        self.is_root_folder = context_state.is_portal_root()
        self.root_url = portal_state.navigation_root_url()
        try:
           self.tab_url = self.context.restrictedTraverse(self.selected_portal_tab).absolute_url()
        except:
           self.tab_url = self.root_url 
	     
class TopBannerViewlet(ViewletBase):
    render = ViewPageTemplateFile('top_banner.pt')
    
    def update(self):
        context_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_context_state')
        self.is_root_folder = context_state.is_portal_root()

class LogoViewlet(ViewletBase):
    render = ViewPageTemplateFile('logo.pt')
 
    def update(self):
        context_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_context_state')
        super(LogoViewlet, self).update()

        portal = self.portal_state.portal()
        logoName = portal.restrictedTraverse('base_properties').logoName
        
        self.portal_title = self.portal_state.portal_title()

        if 'legno-di-marca' in context_state.current_base_url():
           self.logo_tag = portal.restrictedTraverse('lg_legno.gif').tag()
           self.navigation_root_url = portal.restrictedTraverse('legno-di-marca').absolute_url()
        elif 'my-house' in context_state.current_base_url():
           self.logo_tag = portal.restrictedTraverse('lg_mh.gif').tag()
           self.navigation_root_url = portal.restrictedTraverse('my-house').absolute_url()
        elif 'natural-building' in context_state.current_base_url():
           self.logo_tag = portal.restrictedTraverse('lg_nb.gif').tag()
           self.navigation_root_url = portal.restrictedTraverse('natural-building').absolute_url()
        else:
	        self.logo_tag = portal.restrictedTraverse(logoName).tag()
	        self.navigation_root_url = self.portal_state.navigation_root_url()

class FooterViewlet(ViewletBase):
    render = ViewPageTemplateFile('footer.pt')
    
    def update(self):
        portal_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_portal_state')
        context_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_context_state')
        self.is_gallery = context_state.view_template_id() == 'gallery_view'
        self.anonymous = portal_state.anonymous()
        self.site_url = portal_state.portal_url()
           
